package com.ch.dream.logic.longstory;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import cn.goso.utility.NumberTools;

import com.ch.dream.R;
import com.ch.dream.activity.longstory.DreamContentActivity;
import com.ch.dream.asynctask.SelectDreamRequestAsyncTask;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.dream.bean.DreamstoryBean;
import com.ch.dream.common.CommonResult;
import com.ch.dream.tools.ViewTools;

public class DreamContentLogic extends BaseLogic {
	
	private TextView tvContent;
	private Button btnSelect;
	private ProgressDialog loadingDialog;
	private String longStoryId;
	private String dreamId;
	private String content;
	public DreamContentLogic(DreamContentActivity activity) {
		super(activity);
	}
	
	public DreamContentLogic(DreamContentActivity activity, int layoutId) {
		super(activity, layoutId);
	}
	
	public void initViews(){
		tvContent = (TextView)activity.findViewById(R.id.tvContent);
		btnSelect = (Button)activity.findViewById(R.id.btnSelect);
	}
	
	public void initData(){
		Intent intent = activity.getIntent();
		longStoryId = intent.getStringExtra("longStoryId");
		dreamId = intent.getStringExtra("dreamId");
		content = intent.getStringExtra("content");
	}
	
	public void init(){
		initViews();
		initData();
		tvContent.setText(content);
		btnSelect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DreamstoryBean bean =new DreamstoryBean();
				bean.setLongStoryId(NumberTools.stringToInt(longStoryId));
				bean.setId(NumberTools.stringToInt(dreamId));
				new SelectDreamRequestAsyncTask(activity, new RequestServerListener() {
					@Override
					public void onRequestServerEnd(Object result) {
						loadingDialog.dismiss();
						if(result!=null){
							try{
								JSONObject jsonObject = (JSONObject)result;
								if(CommonResult.SUCCESS.equals(jsonObject.getString("result"))){
									ViewTools.showShortToast(activity, "选择成功");
									activity.finish();
								}
							}catch (Exception e) {
								ViewTools.showShortToast(activity, "网络错误");
							}
						}else{
							ViewTools.showShortToast(activity, "网络错误");
						}
					}
					
					@Override
					public void onRequestServerBegin() {
						loadingDialog =ViewTools.showLoading(activity,"提示", "提交数据中，请稍候...");
					}
				}).execute(bean);
			}
		});
	}
	
	
}
